package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class AddRemovePurchase extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2140686958435417008L;
	private Manager manager;
	// private JFrame main;
	
	private JButton  rePur;
	private JButton rePurItem;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField receiptText, upcText;
	

	public AddRemovePurchase(Manager manager) {
		this.manager = manager;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();

	}

	private void addPanel() {
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(4, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		adder.add(new JLabel("Receipt ID: "));
		receiptText = new JTextField();
		adder.add(receiptText);

				
		adder.add(new JLabel("upc (only needed for deleting PurchaseItem:"));
		upcText = new JTextField();
		adder.add(upcText);
		
		/*
		
		adder.add(new JLabel("store Name :"));
		storeNameText = new JTextField();
		adder.add(storeNameText);
		
		adder.add(new JLabel("cardNumber :"));
		cardNumberText = new JTextField();
		adder.add(cardNumberText);
		
		adder.add(new JLabel("expireDate :"));
		expireDateText = new JTextField();
		adder.add(expireDateText);
		
		adder.add(new JLabel("expectedDeliveredDate: "));
		expectedDateText = new JTextField();
		adder.add(expectedDateText);
		
		adder.add(new JLabel("delieveredDate :"));
		deliveredDateText = new JTextField();
		adder.add(deliveredDateText);
*/
		adder.add(new JLabel("Click to remove Purchase"));
		rePur = new JButton("remove");
		rePur.addActionListener(new reShip());
		adder.add(rePur);
		
		adder.add(new JLabel ("Click to remove PurchaseItem"));
		rePurItem = new JButton ("remove");
		rePurItem.addActionListener(new rePurItem());
		adder.add(rePurItem);

		add(adder);
	}

	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED),
				"Confirmation");
		conPanel.setBorder(title);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		confirmation = new JScrollPane(textArea);
		confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		conPanel.add(confirmation, BorderLayout.CENTER);

		add(conPanel);
	}

	
	public class reShip implements ActionListener {
		int sid;

		public void actionPerformed(ActionEvent arg0) {
			sid = Integer.valueOf(receiptText.getText());
			
			try {
				manager.deletePurchase(sid);
				textArea.append("******************* Purchase Deleted ******************" + "\n"
								+ "\n" + "Shipitem with ReceiptID: " + sid + " has been removed successfully"
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipitem does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}

	}
	public class rePurItem implements ActionListener {
		int sid;
		String upc;
		public void actionPerformed(ActionEvent arg0) {
			sid = Integer.valueOf(receiptText.getText());
			upc = upcText.getText();
			
			try {
				manager.deletePurchaseItem(sid, upc);
				textArea.append("******************* PurchaseItem Deleted ******************" + "\n"
								+ "\n" + "PurchaseItem with receiptID: " + sid + " has been removed successfully"
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("Shipitem does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}}

}


